package Demo1;

import java.util.Queue;

public class Test {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();

        myQueue.offer(10);
        myQueue.offer(20);
        myQueue.offer(30);
        myQueue.offer(40);

        System.out.println(myQueue.peek());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.peek());

    }
    public int[] arr;
    public int front;
    public int rear;

    public MyCircularQueue(int k) {
        arr = new int[k + 1];
    }

    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }else {
            arr[rear] = value;

            rear = (rear + 1) % arr.length;

            return true;
        }
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }else {
            front = (front + 1) % arr.length;

            return true;
        }
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }else {
            return arr[front];
        }
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }else {
            int index = rear == 0 ? arr.length - 1 : rear - 1;

            return arr[index];
        }
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        if((rear+1) % arr.length == front) {
            return true;
        }
        return false;
    }
    public int[] arr;
    public int front;
    public int rear;
    public boolean isfull;

    public MyCircularQueue(int k) {
        arr = new int[k];
    }

    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }else {
            arr[rear] = value;

            rear = (rear + 1) % arr.length;
            if(front == rear) {
                isfull = true;
            }

            return true;
        }
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }else {
            front = (front + 1) % arr.length;
            isfull = false;

            return true;
        }
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }else {
            return arr[front];
        }
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }else {
            int index = rear == 0 ? arr.length - 1 : rear - 1;

            return arr[index];
        }
    }

    public boolean isEmpty() {
        return !isFull() && front == rear;
    }

    public boolean isFull() {
        return isfull;
    }
}
